//
//  CityViewController.m
//  search
//
//  Created by 蔡万鸿 on 15/7/11.
//  Copyright (c) 2015年 黄花菜. All rights reserved.
//

#import "CityViewController.h"
#import "CityGroupModel.h"
#import "MJExtension.h"
#import "CitySearchViewController.h"
#import "UIView+AutoLayout.h"

#define MTColor(r,g,b) [UIColor colorWithRed:(r) / 255.0 green:(g) / 255.0 blue:(b) / 255.0 alpha:1]

@interface CityViewController ()<UITableViewDataSource,UITableViewDelegate,UISearchBarDelegate>
//** 城市分组数据*/
@property (nonatomic, strong) NSArray *cityGroups;

@property (weak, nonatomic) IBOutlet UISearchBar *searchBar;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIButton *coverButton;

/** 搜索结果控制器*/
@property (nonatomic, weak) CitySearchViewController *citySearchVC;
@end

@implementation CityViewController

- (CitySearchViewController *)citySearchVC
{
    if (_citySearchVC == nil) {
        //1. 创建
        CitySearchViewController *citySearchVC = [CitySearchViewController new];
        
        //2. 添加控制器-->强引用
        [self addChildViewController:citySearchVC];
        
        //3. 添加view
        [self.view addSubview:citySearchVC.view];
        
        //4. 布局
        //        [citySearchVC.view mas_makeConstraints:^(MASConstraintMaker *make) {
        //            make.edges.equalTo(self.tableView);
        //        }];
        
        // 跟父视图保持"0"间距
        //        [citySearchVC.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero];
        
        
        // 跟父视图保持"0"间距, 除去某条边;
        [citySearchVC.view autoPinEdgesToSuperviewEdgesWithInsets:UIEdgeInsetsZero excludingEdge:ALEdgeTop];
        
        // 布局顶部
        [citySearchVC.view autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.searchBar];
        
        //5. 赋值
        _citySearchVC = citySearchVC;
    }
    return _citySearchVC;
}


- (void)viewDidLoad {
    [super viewDidLoad];
    
    // 设置导航栏
    self.title = @"切换城市";
    
    // 加载城市分组数据
    self.cityGroups = [CityGroupModel objectArrayWithFilename:@"cityGroups.plist"];
    
    // 设置颜色
    self.tableView.sectionIndexColor = MTColor(21, 188, 173);
    self.searchBar.tintColor = MTColor(21, 188, 173);
}

#pragma mark - searchBar代理方法
#pragma mark  searchBar代理方法 开始编辑
- (void)searchBarTextDidBeginEditing:(UISearchBar *)searchBar
{
    //1. 导航栏
    self.navigationController.navigationBarHidden = YES;
    
    //2. 搜索框背景
    [searchBar setBackgroundImage:[UIImage imageNamed:@"bg_login_textfield_hl"]];
    
    //3. 取消按钮
    [searchBar setShowsCancelButton:YES animated:YES];
    
    //目的: 拿到系统的取消按钮, 然后设置成中文
    for (UIView *subView in [searchBar.subviews.firstObject subviews] ) {
        
        //遍历出子视图后, 有一个UINavigationButton, 他一定是继承UIButton
        if ([subView isKindOfClass:[UIButton class]]) {
            
            //强行设置title
            [(UIButton *)subView setTitle:@"取消" forState:UIControlStateNormal];
        }
    }
    
    //4. 遮盖
    self.coverButton.alpha = 0.5;
}

#pragma mark  searchBar代理方法 结束编辑
- (void)searchBarTextDidEndEditing:(UISearchBar *)searchBar
{
    //1. 导航栏
    self.navigationController.navigationBarHidden = NO;
    
    //2. 搜索框背景
    [searchBar setBackgroundImage:[UIImage imageNamed:@"bg_login_textfield"]];
    
    //3. 取消按钮
    [searchBar setShowsCancelButton:NO animated:YES];
    
    //4. 遮盖
    self.coverButton.alpha = 0;
    
    //5. 隐藏搜索结果
    self.citySearchVC.view.hidden = YES;
    
    //6. 清除搜索框文字
    searchBar.text = @"";
}

#pragma mark  searchBar代理方法 文字改变
- (void)searchBar:(UISearchBar *)searchBar textDidChange:(NSString *)searchText
{
    if (searchText.length) {
        self.citySearchVC.view.hidden = NO;
        // 搜索结果传递
        self.citySearchVC.searchText = searchText;
    } else {
        self.citySearchVC.view.hidden = YES;
    }
}

#pragma mark  searchBar代理方法 取消按钮点击
- (void)searchBarCancelButtonClicked:(UISearchBar *)searchBar
{
    [searchBar resignFirstResponder];
}

#pragma mark 点击遮盖消息
- (IBAction)coverDismiss
{
    [self.searchBar resignFirstResponder];
}


#pragma mark - 数据源方法
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return self.cityGroups.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // 先获取数据模型
    CityGroupModel *cityGroupModel = self.cityGroups[section];
    return cityGroupModel.cities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *ID = @"cell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:ID];
    }
    // 先获取数据模型
    CityGroupModel *cityGroupModel = self.cityGroups[indexPath.section];
    //获取对应额城市数据
    cell.textLabel.text = cityGroupModel.cities[indexPath.row];
    
    return cell;
}

#pragma mark 返回分组的标题
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    // 先获取数据模型
    CityGroupModel *cityGroupModel = self.cityGroups[section];
    
    return cityGroupModel.title;
}

#pragma mark 索引列表
- (NSArray *)sectionIndexTitlesForTableView:(UITableView *)tableView
{
    // 获取每个模型的title-->可以遍历获取-->临时数据保存遍历的结果-->遍历的结果就是我们需要的数据
    //    NSMutableArray *tempArray = [NSMutableArray array];
    //
    //    for (MTCityGroupModel *cityGroupModel in self.cityGroups) {
    //        [tempArray addObject:cityGroupModel.title];
    //    }
    //    return tempArray;
    
    //使用KVC方法, 快速返回
    return [self.cityGroups valueForKey:@"title"];
}

@end
